package com.example.cf.grouplist.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.cf.grouplist.R;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import static com.example.cf.grouplist.R.id.tv;

public class GroupRefreshActivity extends AppCompatActivity {

    private PullToRefreshListView lv;
    private List<Data> data;
    private MAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_refresh);
        lv = ((PullToRefreshListView) findViewById(R.id.lv));
        initData();
        mAdapter = new MAdapter();
        lv.setAdapter(mAdapter);
        lv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.e("TAG",lv.getChildAt(0).getY()+"");
            }
        });
    }

    private void initData() {
        data=new ArrayList<>();
        for (int i=0;i<100;i++){
            Data d = new Data();
            d.group="我是头"+(i/10);
            d.name="item"+i;
            data.add(d);
        }
    }

    private class MAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if (convertView==null){
                holder=new ViewHolder();
                convertView=getLayoutInflater().inflate(R.layout.item,null);
                holder.head= (LinearLayout) convertView.findViewById(R.id.head);
                holder.tv= (TextView) convertView.findViewById(tv);
                holder.head_tv= (TextView) convertView.findViewById(R.id.head_tv);
                convertView.setTag(holder);
            }else
                holder= (ViewHolder) convertView.getTag();
            holder.tv.setText(data.get(position).name);
            if (position==0||!data.get(position).group.equals(data.get(position-1).group)){
                holder.head_tv.setVisibility(View.VISIBLE);
                holder.head_tv.setText(data.get(position).group);
            }else
                holder.head_tv.setVisibility(View.GONE);
            return convertView;
        }
        class ViewHolder{
            LinearLayout head;
            TextView tv,head_tv;
        }
    }
    class Data{
        public String group;
        public String name;
    }
}
